import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import com.google.code.ptrends.Application.DAL.RepositoryServiceBean;
import com.google.code.ptrends.Application.DAL.entities.ItemEntity;

@ManagedBean(name = "repositoryBean")
@ViewScoped
public class RepositoryBean implements Serializable {

	private static final long serialVersionUID = 1L;

	@EJB
	RepositoryServiceBean beanService;

	private List<String> suppliers;
	private List<String> categories;
	private List<String> manufacturers;
	private List<String> classes;
	private String selectedSupplier;
	private String selectedCategory;
	private String selectedManufacturer;
	private String selectedClass;
	private List<ItemEntity> items;
	
	private int progress;

	public void setSuppliers(List<String> suppliers) {
		this.suppliers = suppliers;
	}

	public List<String> getSuppliers() {
		loadSuppliers();
		return suppliers;
	}

	public void setCategories(List<String> categories) {
		this.categories = categories;
	}

	public List<String> getCategories() {
		loadCategories();
		return categories;
	}

	public void setManufacturers(List<String> manufacturers) {
		this.manufacturers = manufacturers;
	}

	public List<String> getManufacturers() {
		loadManufacturers();
		return manufacturers;
	}

	public void setClasses(List<String> classes) {
		this.classes = classes;
	}

	public List<String> getClasses() {
		loadClasses();
		return classes;
	}

	public void setSelectedSupplier(String selectedSupplier) {
		this.selectedSupplier = selectedSupplier;
	}

	public String getSelectedSupplier() {
		return selectedSupplier;
	}

	public void setSelectedCategory(String selectedCategory) {
		this.selectedCategory = selectedCategory;
	}

	public String getSelectedCategory() {
		return selectedCategory;
	}

	public void setSelectedManufacturer(String selectedManufacturer) {
		this.selectedManufacturer = selectedManufacturer;
	}

	public String getSelectedManufacturer() {
		return selectedManufacturer;
	}

	public void setSelectedClass(String selectedClass) {
		this.selectedClass = selectedClass;
	}

	public String getSelectedClass() {
		return selectedClass;
	}

	private void loadSuppliers() {
		if (this.suppliers == null) {
			this.setSuppliers(beanService.getAllSuppliers());
		}
	}

	private void loadCategories() {
		if (this.categories == null) {
			this.categories = beanService.getAllCategories();
		}
	}

	private void loadManufacturers() {
		this.manufacturers = beanService
				.getAllManufacturers(this.selectedCategory);
	}

	private void loadClasses() {
		if (this.classes == null) {
			this.classes = beanService.getAllClasses();
		}
	}

	public void loadItems(ActionEvent actionEvent) {
		if (progress != 0)
			progress = 0;
		this.progress = 20;	
		this.items = beanService.getItemsByParameters(this.selectedSupplier,
				this.selectedCategory, this.selectedManufacturer,
				this.selectedClass);
		this.progress = 100;
	}

	public void setItems(List<ItemEntity> items) {
		this.items = items;
	}

	public List<ItemEntity> getItems() {
		return items;
	}
	
	public void onComplete() {
		FacesContext.getCurrentInstance().addMessage(null,
				new FacesMessage("Load compleate"));
	}
	
	public void setProgress(Integer progress) {
		this.progress = progress;
	}

	public Integer getProgress() {
		return progress;
	}

}